package com.xiaolun.layui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaolun.layui.entity.Product;
import com.xiaolun.layui.entity.ProductCategory;
import com.xiaolun.layui.mapper.ProductCategoryMapper;
import com.xiaolun.layui.mapper.ProductMapper;
import com.xiaolun.layui.service.ProductService;
import com.xiaolun.layui.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private ProductCategoryMapper productCategoryMapper;

	@Override
	public DataVO<ProductVO> findData(Integer page, Integer limit) {
		DataVO dataVO = new DataVO();
		dataVO.setCode(0);
		dataVO.setMsg("");

		/**
		 * 1、设置分页条件，<Product>定义的泛型
		 * 2、设置拦截器 MyBatisPlusConfig（com.xiaolun.layui.config）才能进行分页处理
		 */
		IPage<Product> productIPage = new Page<>(page, limit);
		IPage<Product> result = productMapper.selectPage(productIPage, null);
		dataVO.setCount(result.getTotal());

		// 将Product实体类型转换为ProductVO类型
		List<Product> productList = result.getRecords();
		List<ProductVO> productVOList = new ArrayList<>();

		for (Product product : productList) {
			ProductVO productVO = new ProductVO();
			/**
			 * 将product数据传递给productVO,但是下面三个属性（三个分类）无法对应：
			 * categorylevelone;
			 * categoryleveltwo;
			 * categorylevelthree;
			 * 需要手动输入
			 */
			BeanUtils.copyProperties(product, productVO);

			QueryWrapper wrapper = new QueryWrapper();
			wrapper.eq("id", product.getCategoryleveloneId());
			ProductCategory productCategory = productCategoryMapper.selectOne(wrapper);
			if (productCategory != null) {
				productVO.setCategorylevelone(productCategory.getName());
			}

			wrapper = new QueryWrapper();
			wrapper.eq("id", product.getCategoryleveltwoId());
			productCategory = productCategoryMapper.selectOne(wrapper);
			if (productCategory != null) {
				productVO.setCategoryleveltwo(productCategory.getName());
			}

			wrapper = new QueryWrapper();
			wrapper.eq("id", product.getCategorylevelthreeId());
			productCategory = productCategoryMapper.selectOne(wrapper);
			if (productCategory != null) {
				productVO.setCategorylevelthree(productCategory.getName());
			}
			productVOList.add(productVO);
		}
		dataVO.setData(productVOList);
		return dataVO;
	}

	@Override
	public BarVO getBarVO() {
		List<ProductBarVO> list = productMapper.findAllProductBarVO();
		List<String> names = new ArrayList<>();
		List<Integer> values = new ArrayList<>();
		for (ProductBarVO productBarVO : list) {
			names.add(productBarVO.getName());
			values.add(productBarVO.getCount());
		}
		BarVO barVO = new BarVO();
		barVO.setNames(names);
		barVO.setValues(values);
		return barVO;
	}

	@Override
	public List<PieVO> getPieVO() {
		List<ProductBarVO> list = productMapper.findAllProductBarVO();
		List<PieVO> pieVOList = list.stream()
				.map(e -> new PieVO(
						e.getCount(),
						e.getName()
				)).collect(Collectors.toList());
		return pieVOList;
	}
}
